/**
 * 
 */

function checkStatusLimit() {
	if (document.getElementsByName("status")[0].value != "") {
		if (document.getElementsByName("status")[0].value.length < 400) {
			document.forms[0].submit();
		} else {
			alert("Status containing more than 400 words is not allowed");
		}
	} else {
		alert("Empty status can't be updated");
	}
}
function setUserId(id) {
	if (confirm("Do you want to delete?")) {
		document.getElementsByName("statusId")[0].value = id;
		document.forms[1].submit();
	}
}

function setComment(id, comment) {
	if (document.getElementsByName(comment)[0].value != "") {
		if (document.getElementsByName(comment)[0].value.length < 400) {
			document.getElementsByName("statusId")[0].value = id;
			document.getElementsByName("comment")[0].value = document
					.getElementsByName(comment)[0].value;
			document.forms[1].submit();
		} else {
			alert("Comment containing more than 400 words is not allowed");
		}
	} else {
		alert("Empty comment can't be updated");
	}
}

function setCommentId(commentId) {
	if (confirm("Do you want to delete?")) {
		document.getElementsByName("commentId")[0].value = commentId;
		document.forms[1].submit();
	}

}

function statusBlur() {
	var status = document.getElementByName("status")[0].value;
	if (status.trim() == "") {
		document.getElementByName("status")[0].value = "Update Status";
		document.getElementByName("status")[0].style.color = "#666666";
	} else {
		document.getElementByName("status")[0].style.color = "#000000";
	}
}

function statusFocus() {
	var status = document.getElementByName("status")[0].value;
	if (status == "Update Status") {
		document.getElementByName("status")[0].value = "";
		document.getElementByName("status")[0].style.color = "#000000";
	}
}

function FuncWaterMark(obj, event) {
	var strVal = obj.defaultValue;
	// Here to check textbox length and event type
	if (obj.value.length == 0 & event.type == "blur") {
		obj.style.color = "#D0CFCF";// setting text color
		obj.value = strVal; // setting default text in textbox
	}
	// Here to check textbox value and event type
	if (obj.value == strVal & event.type == "focus") {
		obj.style.color = "black";
		obj.value = "";
	}
}

function validateEmailId(obj) {
	var emailAddress = obj.emailId.value;
	var defaultemailAddress = obj.emailId.defaultValue;
	if (emailAddress == "" || emailAddress == null
			|| emailAddress == defaultemailAddress) {
		document.getElementById("alert").innerHTML = "Please Enter a Valid Email Address";
		return false;
	} else {
		emailAddress = emailAddress.toLowerCase();
		if (strEndsWith(emailAddress, "@metacube.com")
				|| strEndsWith(emailAddress, "@metagurukul.com")) {
			document.getElementById("alert").innerHTML = "";
			return true;
		} else {
			document.getElementById("alert").innerHTML = "Insert Valid Domain Name";
			return false;
		}
	}
}
function strEndsWith(str, suffix) {
	return str.match(suffix + "$") == suffix;
}
function validateMobile(mobile) {
	var pattern = /^([0-9]){10}$/; // regular expression for mobile no pattern

	if (pattern.test(mobile)) // if mobile no entered by user matches this
								// pattern
	{
		return true;
	} else {
		return false;
	}
}
function validateLogin(obj) {
	var username = obj.userName.value;
	var password = obj.password.value;
	var defaultusername = obj.userName.defaultValue;
	var defaultpassword = obj.password.defaultValue;
	if (username == "" || username == null || username == defaultusername
			|| password == "" || password == null
			|| password == defaultpassword) {
		document.getElementById("alert").innerHTML = "Please Fill Details";
		return false;
	} else {
		document.getElementById("alert").innerHTML = "";
		return true;
	}
}

function validatePersonalDetails(obj) {
	var ck_name = /^[A-Za-z ]{1,100}$/;
	var ck_username = /^[A-Za-z0-9_]{1,50}$/;
	var username = obj.userName.value;
	var name = obj.name.value;
	var contact = obj.contact.value;
	var date = obj.date.value;
	var gender_male = obj.gender[0].checked;
	var gender_female = obj.gender[1].checked;
	var flag = true;
	if (username == "" || username == null) {
		document.getElementById("error_userName").innerHTML = "Please Enter UserName";
		flag = false;
	} else {
		if (!ck_username.test(username)) {
			document.getElementById("error_userName").innerHTML = "UserName may contain only characters and digits";
			flag = false;
		} else {
			document.getElementById("error_userName").innerHTML = "";
		}
	}
	if (name == "" || name == null) {
		document.getElementById("error_name").innerHTML = "Please Enter Name";
		flag = false;
	} else {
		if (!ck_name.test(name)) {
			document.getElementById("error_name").innerHTML = "Name may contain only characters";
			flag = false;
		} else {
			document.getElementById("error_name").innerHTML = "";
		}
	}
	if (contact == "" || contact == null) {
		document.getElementById("error_contact").innerHTML = "Please Enter Contact No.";
		flag = false;
	} else {
		if (validateMobile(contact)) {
			document.getElementById("error_contact").innerHTML = "";
		} else {
			document.getElementById("error_contact").innerHTML = "Contact No Should Contain 10 digits.";
			flag = false;
		}
	}
	if (gender_male == false && gender_female == false) {
		document.getElementById("error_gender").innerHTML = "Please Select a Gender.";
		flag = false;
	} else {
		document.getElementById("error_gender").innerHTML = "";
	}
	if (date == "" || date == null) {
		document.getElementById("error_date").innerHTML = "Please Select a Date.";
		flag = false;
	} else {
		
		if(validateDate(date))
		{
			document.getElementById("error_date").innerHTML = "";
		}
		else
		{
			document.getElementById("error_date").innerHTML = "Invalid Date Of Birth.";
			flag = false;
		}
		
	}
	if (flag) {
		return true;
	} else {
		return false;
	}
}

function validateWorkDetails(obj) {
	var department = obj.department.value;
	var project = obj.project.value;
	var role = obj.role.value;
	var flag = true;

	if (department == null || department == "-1") {
		document.getElementById("error_department").innerHTML = "Please Select department";
		flag = false;
	} else {
		document.getElementById("error_department").innerHTML = "";
	}
	if (project == null || project == "-1") {
		document.getElementById("error_project").innerHTML = "Please Select project";
		flag = false;
	} else {
		document.getElementById("error_project").innerHTML = "";
	}
	if (role == null || role == "-1") {
		document.getElementById("error_role").innerHTML = "Please Select role";
		flag = false;
	} else {
		document.getElementById("error_role").innerHTML = "";
	}
	if (flag) {
		return true;
	} else {
		return false;
	}
}

function validateChangePassword(obj) {
	var ck_password = /^((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,100})$/;
	var oldPassword = obj.oldPassword.value;
	var newPassword = obj.newPassword.value;
	var confirmPassword = obj.confirmPassword.value;

	if (oldPassword == "" || oldPassword == null) {
		document.getElementById("error_oldPassword").innerHTML = "Please Enter Old Password.";
		flag = false;
	} else {
		document.getElementById("error_oldPassword").innerHTML = "";
	}
	if (newPassword == "" || newPassword == null) {
		document.getElementById("error_newPassword").innerHTML = "Please Enter New Password.";
		flag = false;
	} else {
		if (!ck_password.test(newPassword)) {
			document.getElementById("error_newPassword").innerHTML = "Password length must be of minimum 6 characters contains atleast one uppercase, one lowercase character and one digit.";
			flag = false;
		} else {
			document.getElementById("error_newPassword").innerHTML = "";
		}
	}
	if (confirmPassword == "" || confirmPassword == null) {
		document.getElementById("error_confirmPassword").innerHTML = "Please Confirm New Password.";
		flag = false;
	} else {
		if (newPassword != confirmPassword) {
			document.getElementById("error_confirmPassword").innerHTML = "New Password and Confirm Password fields are different.";
			flag = false;
		} else {
			document.getElementById("error_confirmPassword").innerHTML = "";
		}
	}
	if (flag) {
		return true;
	} else {
		return false;
	}
}

function validateUploadPicture(obj) {
	var fileUpload = obj.selectFile;
	var fileName = fileUpload.value; // get the name of the file uploaded by
										// user
	var extension = fileName.substring(fileName.lastIndexOf('.') + 1); // get
																		// extension
																		// of
																		// the
																		// file
	if (extension == "jpeg" || extension == "jpg" || extension == "png") // match
																			// the
																			// extension
	{
		if (fileUpload != null) // if extension is valid then calculate size
		{
			if (fileUpload.files != null) {
				var check = fileUpload.files[0];
				if (check != null) {
					var checkSize = check.size;
					if (checkSize / 1024 > 300) // if size is greater than 100
												// kb then show error.
					{
						document.getElementById("error_picture").innerHTML = 'File Size too Big.';
						return false;
					} else {
						document.getElementById("error_picture").innerHTML = '';
						return true;
					}
				}
			}
		}
	} else {
		document.getElementById("error_picture").innerHTML = 'Please Upload Image With Valid Format.';
		return false;
	}
}
function validateDate(date)
{
	var curDate = new Date();
	var curDay = curDate.getDate();
	var curMonth = curDate.getMonth()+1;
	var curYear = getRealYear(curDate);
	var n=date.split("-");
	if(n[2] > curYear)
	{
		return false;
	}
	else if(n[2]==curYear)
	{
		if(n[1] > curMonth)
		{
			return false;
		}
		else if(n[1] == curMonth)
		{
			if(n[0] > curDay)
			{
				return false;;
			}
			else
			{
				return true;
			}
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
